﻿'use strict';

if (zxcv && !zxcv.TransformNode) {
    zxcv.TransformNode = function () {
        this.name = '';
        this.scaleX = 1;
        this.scaleY = 1;
        this.rotationX = 0;
        this.rotationY = 0;
        this.rotationZ = 0;
        this.translationX = 0;
        this.translationY = 0;
        this.isActive = true;
        this.group = null;
        this.matrix = mat4.create();
    };

    zxcv.TransformNode.prototype.translate = function (x, y) {
        this.translationX += x;
        this.translationY += y;
    };

    zxcv.TransformNode.prototype.translateTo = function (x, y) {
        if (x)
            this.translationX = x;
        if (y)
            this.translationY = y;
    };

    zxcv.TransformNode.prototype.rotate = function (r) {
        this.rotationZ += r;
    };

    zxcv.TransformNode.prototype.rotateTo = function (r) {
        this.rotationZ = r; 
    };

    zxcv.TransformNode.prototype.updateTransform = function () {
        if (this.group)
            mat4.set(this.group.matrix, this.matrix);
        else
            mat4.identity(this.matrix);

        mat4.translate(this.matrix, [this.translationX, this.translationY, 0]);
        mat4.rotate(this.matrix, this.rotationZ, [0, 0, 1]);
        mat4.scale(this.matrix, [this.scaleX, this.scaleY, 1]);
    };

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    zxcv.TransformNodeGroup = function () {
        zxcv.TransformNode.apply(this);
        this.children = [];
    };
    zxcv.TransformNodeGroup.prototype.translate   = zxcv.TransformNode.prototype.translate;
    zxcv.TransformNodeGroup.prototype.translateTo = zxcv.TransformNode.prototype.translateTo;
    zxcv.TransformNodeGroup.prototype.rotate      = zxcv.TransformNode.prototype.rotate;
    zxcv.TransformNodeGroup.prototype.rotateTo    = zxcv.TransformNode.prototype.rotateTo;
    zxcv.TransformNodeGroup.prototype.updateTransform = function () {
        zxcv.TransformNode.prototype.updateTransform.call(this);

        var i, length,
            children = this.children;

        for (i = 0, length = children.length; i < length; i++)
            children[i].updateTransform();
    };

    zxcv.TransformNodeGroup.prototype.create = function () {
        this.children.push(new zxcv.TransformNode());
    };

    zxcv.TransformNodeGroup.prototype.createGroup = function () {
        var item = new zxcv.TransformNodeGroup();
        item.group = this;
        this.children.push(item);
        return item;
    };

    zxcv.TransformNodeGroup.prototype.createHierarchy = function () {
    };

    zxcv.TransformNodeGroup.prototype.remove = function (item) {
    };

    zxcv.TransformNodeGroup.prototype.removeAt = function (index) {
    };

    zxcv.TransformNodeGroup.prototype.clear = function () {
    };
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    zxcv.TransformNodeHierarchy = function () {
        zxcv.TransformNode.apply(this);
    };
};